{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([41, 93, 10, 50, 76, 27, 70, 47, 33, 82])"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "nums = np.random.randint(1,100,(10,))\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[10, 27, 33, 41, 47, 50, 70, 76, 82, 93]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 以递增方式排序数组，添加有序信息：两数之和Ⅰ无序数组算法依然可以使用，但存在时空复杂度更低的算法\n",
    "nums = sorted(nums)\n",
    "nums"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "115"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "target = nums[2] + nums[8]\n",
    "target"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#二分法：有序提示对于每个匹配对的另一值可以直接在当前值右侧序列里二分原地查询\n",
    "def algorithm1(nums, target):\n",
    "    for i in range(len(nums)):         # 从左到右遍历数组每个元素\n",
    "        low, high = i+1, len(nums)-1   # 定二分法区间:排除当前值前所有后的，右侧整个序列\n",
    "        while low <= high:\n",
    "            mid = int((low+high)//2)\n",
    "            if nums[mid] == target - nums[i]:\n",
    "                return [i, mid]\n",
    "            elif nums[mid] > target - nums[i]:\n",
    "                high = mid - 1\n",
    "            else:\n",
    "                low = mid + 1\n",
    "    return [-1,-1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 8]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "algorithm1(nums, target)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "# 有序提示可以使用双指针，从最小和最大的数慢慢“夹逼”到目标值\n",
    "def algorithm2(nums, target):\n",
    "    low, high = 0, len(nums)-1\n",
    "    while low<high:\n",
    "        total = nums[low] + nums[high]\n",
    "        if target == total:\n",
    "            return [low, high]\n",
    "        elif target < total:          # 当目标值比当前小时，考虑使两数和变小\n",
    "            high -= 1                 # 只有右侧大数指针向左移一位，可以减小两数和\n",
    "        else:                         # 当目标值比当前大时，考虑使两数和变大\n",
    "            low += 1                  # 只有左侧小数指针向右移位，可以增大两数和\n",
    "    return [-1,-1]  "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[2, 8]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "algorithm2(nums, target)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
